package lil.llahn.security.common;

import lil.llahn.security.common.asymmetric.DHSecurityTools;
import lil.llahn.security.common.symmetry.AESSecurityTools;
import lil.llahn.security.common.symmetry.DESSecurityTools;
import lil.llahn.security.common.symmetry.PBESecurityTools;

import java.util.Map;

/**
 * @ClassName: TestTool
 * @Description: TODO
 * @Date: 2019/11/8 16:40
 * @Auther: lil
 */
public class TestTool {
    private static final String data = "zhangsan/123456";

    public static void main(String[] args) {
        System.out.println("初始化的待加密字符串为：" + data);
//        des();
//        pbe();
//        aes();
        dh();
    }

    private static void des() {
        System.out.println("__START_TEST_DES________________________________________________");
        String key = DESSecurityTools.init();
        System.out.println("密    钥：" + key);
        long start = System.currentTimeMillis();
        String dd = DESSecurityTools.encryptDESByKey(data, key);
        System.out.println("密    文：" + dd);
        System.out.println("重获明文：" + DESSecurityTools.decryptDESByKey(dd, key));
        System.out.println("总  耗时："+(System.currentTimeMillis() - start)+" 毫秒。");
    }

    private static void pbe() {
        System.out.println("__START_TEST_PBE________________________________________________");
        long start = System.currentTimeMillis();
        String password = "password";
        System.out.println("密码设置为：" + password);
        byte[] salt = PBESecurityTools.init();
        String dd = PBESecurityTools.encryptPBE(data, password, salt);
        System.out.println("密    文：" + dd);
        System.out.println("重获明文：" + PBESecurityTools.decryptPBE(dd, password, salt));
        System.out.println("总  耗时："+(System.currentTimeMillis() - start)+" 毫秒。");
    }


    private static void aes() {
        System.out.println("__START_TEST_AES________________________________________________");
        long start = System.currentTimeMillis();
        String password = "password";
        System.out.println("密码设置为：" + password);
        String dd = AESSecurityTools.encryptAESByKey(data, password);
        System.out.println("密    文：" + dd);
        System.out.println("重获明文：" + AESSecurityTools.decryptAESByKey(dd, password));
        System.out.println("总  耗时："+(System.currentTimeMillis() - start)+" 毫秒。");
    }


    private static void dh(){
        System.out.println("__START_TEST_DH_________________________________________________");
        long start = System.currentTimeMillis();
        Map<Integer, String> a = DHSecurityTools.init();
        System.out.println("甲方公钥：" + a.get(0));
        System.out.println("甲方私钥：" + a.get(1));
        Map<Integer, String> b = DHSecurityTools.init(a.get(0));
        System.out.println("乙方公钥：" + b.get(0));
        System.out.println("乙方私钥：" + b.get(1));

        String ss = DHSecurityTools.encrypt(data, a.get(0), b.get(1));
        System.out.println("密文：" + ss);
        System.out.println("密文解密：" + DHSecurityTools.decrypt(ss, b.get(0), a.get(1)));

    }
}
